package control.hall
{
	import control.panel.PanelControl;
	
	import events.HallEvent;
	
	import model.hall.ListData;
	
	import view.hall.PlayerList;

	public class PlayerListControl extends PanelControl
	{
		private var playerList:PlayerList;
		private var currentRoomId:String = "-1";
		private var currentServerId:String;
		private var currentIndex:int = -1;
		
		public function PlayerListControl()
		{
			initList();
			initListener();
		}
		
		private function initListener():void
		{
			_model.hallModel.addEventListener(HallEvent.ROOM_PLAYER_LIST_COMPLETE,onRoomPlayerListComplete);
			_model.hallModel.addEventListener(HallEvent.FRIEND_LIST_COMPLETE,onFriendListComplete);
		}
		
		private function onFriendListComplete(event:HallEvent):void
		{
			if (currentIndex == event.xmlType)
			{
				updateList(event.xml);	
			}
		}
		
		private function onRoomPlayerListComplete(event:HallEvent):void
		{
			if (currentIndex == event.xmlType)
			{
				updateList(event.xml);	
			}
		}
		
		private function updateList(data:XML):void
		{
			var array:Array = [];
			var listData:ListData;
			for each (var xml:XML in data.children())
			{
				listData = new ListData();
				listData.userName = xml.@name.toString();
				listData.money = xml.@gameMoney.toString();
				//listData.state = getState(xml.@state.toString());	
				listData.state = xml.@position;	
				listData.data = xml;
				listData.headImg = xml.@headImg.toString();
				array.push(listData);
			}
			playerList.hideLoading();
			playerList.update(array);
		}
		
		public function onTabSwitch(index:int):void
		{
			playerList.clear();
			playerList.showLoading();
			currentIndex = index;
			switch(index)
			{
				case 0:
					sendRoomPlayerRequest(currentServerId,currentRoomId);
					break;
				case 1:
				case 2:
					_model.hallModel.sendFriendListRequest(index);
					break;
			}
		}
		
		
		
		public function sendRoomPlayerRequest(serverId:String,roomId:String):void
		{
			
			
			
			if (currentIndex != 0)
			{
				playerList.switchToTab(0);
			}
			
			if (roomId == "-1")
			{
				playerList.hideLoading();
				return;
			}
			
			
			playerList.clear();
			playerList.showLoading();
			currentServerId = serverId;
			currentRoomId = roomId;
			if (serverId && roomId)
			{
				_model.hallModel.sendRoomPlayerListRequest(serverId,roomId,currentIndex);
			}
		}
		
		private function getState(type:String):String
		{
			var state:String  = "";
			if (type == "1")
			{
				state = "游戏中";
			}
			else if (type == "0")
			{
				state = "旁观";
			}
			else if (type == "2")
			{
				state = "离线";
			}
			return state;
		}
		
		
		private function initList():void
		{
			playerList = new PlayerList(this);
			playerList.x = 600;
			playerList.y = 210;
			_view.hallLayer.addChild(playerList);
		}
		
		public function clear():void
		{
			playerList.clear();
			//currentServerId = null;
			//currentRoomId = null;
		}
	}
}